package com.lsh.oms.worker.service.task;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lsh.oms.core.enums.TaskStatus;
import com.lsh.oms.core.enums.TaskType;
import com.lsh.oms.core.exception.BusinessException;
import com.lsh.oms.core.model.http.HttpMessage;
import com.lsh.oms.core.model.order.OrderShippingHeadWithBLOBs;
import com.lsh.oms.core.model.po.OmsTask;
import com.lsh.oms.core.proxy.service.TmsProxy;
import com.lsh.oms.core.service.other.ShippingOrderService;
import com.lsh.oms.worker.service.base.OmsSimpleElasticJob;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;
import java.util.UUID;

/**
 * Project Name: lsh-oms
 *
 * @author peter
 * @date 18/2/27
 * 北京链商电子商务有限公司
 * Package
 * desc:
 */
@Slf4j
@Component
public class OrderShippingNotifyTmsJop extends OmsSimpleElasticJob {

    @Autowired
    private ShippingOrderService shippingService;

    @Autowired
    private TmsProxy tmsProxy;

    @Override
    protected Integer getFetchSize() {
        return 100;
    }

    @Override
    protected TaskType getTaskType() {
        return TaskType.ORDER_PUSH_TMS;
    }

    @Override
    protected List<Integer> dealTaskStatus() {
        return Arrays.asList(TaskStatus.TASK_UN_DEAL.getCode(), TaskStatus.TASK_DEAL_FAIL.getCode());
    }

    @Override
    protected int process(OmsTask omsTask) {
        boolean dealFlag = false;
        try {
            Long orderCode = Long.valueOf(omsTask.getRefCode());

            OrderShippingHeadWithBLOBs orderShippingHead = this.shippingService.getBolbs(orderCode);

            if (null == orderShippingHead) {
                log.info("{} 此订单还没有发货单", orderCode);
                return 0;
            }

            JSONObject warehouseData = JSON.parseObject(orderShippingHead.getWarehouseData());
            Integer deliveryType = warehouseData.getInteger("delivery_type");
            if(null != deliveryType && deliveryType == 4){
                return 1;
            }

            log.info(orderCode + "通知tms发货开始");
            HttpMessage httpMessage = this.tmsProxy.shippedTask(UUID.randomUUID().toString(), orderShippingHead, orderShippingHead.getVenderId());

            log.info(orderCode + "通知tms发货结束" + JSON.toJSONString(httpMessage));
            if (httpMessage.getStatus().equals("200")) {
                OrderShippingHeadWithBLOBs update = new OrderShippingHeadWithBLOBs();
                update.setId(orderShippingHead.getId());

                String message = httpMessage.getMessage();
                if (StringUtils.isBlank(message)) {
                    return 0;
                }

                JSONObject res = JSONObject.parseObject(message);
                if (null == res || res.getInteger("ret") != 0) {
                    return -1;
                }
                JSONObject content = res.getJSONObject("content");
                JSONObject routeInfo = content.getJSONObject("route_info");

                update.setSeq(content.getInteger("seq"));
                update.setWaybillNo(routeInfo.getString("route_id"));
                update.setRouteId(routeInfo.getLong("route_id"));
                update.setTransUid(routeInfo.getLong("trans_uid"));

                log.info("{} 更新发货单信息 {}", orderShippingHead.getShippingOrderId(), JSON.toJSONString(update));
                if (shippingService.updateByPKSelective(update) >= 1) {
                    dealFlag = true;
                }
            }
        } catch (BusinessException ex) {
            log.error(omsTask.getRefCode() + " 通知tms发货业务异常 " + ex.getMessage(), ex);
            return -1;
        } catch (Exception e) {
            log.error(omsTask.getRefCode() + " 通知tms发货异常 " + e.getMessage(), e);
            return -1;
        }

        return dealFlag == true ? 1 : -1;
    }

}
